package com.whc.gulimall.coupon.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.Query;
import com.whc.gulimall.coupon.dao.SeckillSessionDao;
import com.whc.gulimall.coupon.entity.SeckillSessionEntity;
import com.whc.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.whc.gulimall.coupon.service.SeckillSessionService;
import com.whc.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author angaoen
 */
@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    /**
     * 秒杀活动商品关联
     */
    @Resource
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取最近三天需要上架的排表
     *
     * @return 最近三天需要上架的排表
     */
    @Override
    public List<SeckillSessionEntity> listLastThreeDaysSession() {
        // 获取近三天的秒杀活动
        List<SeckillSessionEntity> sessionEntityList = this
                .list(new QueryWrapper<SeckillSessionEntity>().between("start_time", getStartSrt(), getEndSrt()));
        if (CollUtil.isNotEmpty(sessionEntityList)) {
            // 通过活动号获取到关联的sku
            List<SeckillSkuRelationEntity> relationEntityList = seckillSkuRelationService
                    .list(new QueryWrapper<SeckillSkuRelationEntity>()
                            .in("promotion_session_id", sessionEntityList.stream()
                                    .map(SeckillSessionEntity::getId).collect(Collectors.toList())));
            // 通过设置每个活动关联的所有商品
            if (CollUtil.isNotEmpty(relationEntityList)) {
                Map<Long, List<SeckillSkuRelationEntity>> entityMap = relationEntityList.stream()
                        .collect(Collectors.groupingBy(SeckillSkuRelationEntity::getPromotionSessionId));
                for (SeckillSessionEntity seckillSessionEntity : sessionEntityList) {
                    seckillSessionEntity.setRelationEntities(entityMap.get(seckillSessionEntity.getId()));
                }
            }
        }
        return sessionEntityList;
    }

    private String getStartSrt() {
        LocalDateTime start = LocalDateTimeUtil.parse(LocalDateTime.now().toString());
        start = LocalDateTimeUtil.beginOfDay(start);
        return LocalDateTimeUtil.format(start, DatePattern.NORM_DATETIME_PATTERN);
    }

    private String getEndSrt() {
        LocalDateTime end = LocalDateTimeUtil.offset(LocalDateTimeUtil.parse(LocalDateTime.now().toString()),
                3, ChronoUnit.DAYS);
        end = LocalDateTimeUtil.endOfDay(end);
        return LocalDateTimeUtil.format(end, DatePattern.NORM_DATETIME_PATTERN);
    }

}
